/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import javax.swing.JFrame;
//import components.Database;
//import components.JSqlList;
//import java.sql.ResultSet;
import javax.swing.JOptionPane;
import model.Kind;

/**
 *
 * @author Thierry
 */
public class Kindscherm extends javax.swing.JFrame {

    /**
     * Creates new form Kind
     */
    public Kindscherm() { //indien los gerund
        initComponents();
        
        //zorgt dat alleen dit frame sluit, en niet ook de parent
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    
    public Kindscherm(int invoercode) { //indien gerund vanuit een andere class
        initComponents();
        
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //zorgt dat alleen dit frame sluit, en niet ook de parent
        
        fieldOudercode.setText(""+invoercode); //zet code in oudercode veld
        fieldOudernaam.setText(Kind.getNaamOuder(invoercode)); //zet de bijbehorende naam erbij
        
        buttonZoek.doClick(); //zoek de juiste kinderen erbij

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        studentList = new components.JSqlList();
        lblOudercode = new javax.swing.JLabel();
        fieldOudercode = new javax.swing.JTextField();
        buttonZoek = new javax.swing.JButton();
        fieldResultaten = new components.JSqlList();
        buttonVerwijder = new javax.swing.JButton();
        buttonWijzig = new javax.swing.JButton();
        buttonNieuw = new javax.swing.JButton();
        lblNaam = new javax.swing.JLabel();
        lblGeslacht = new javax.swing.JLabel();
        lblGeboortedatum = new javax.swing.JLabel();
        buttonTerug = new javax.swing.JToggleButton();
        lblOudernaam = new javax.swing.JLabel();
        fieldOudernaam = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Kindscherm");

        lblOudercode.setText("Lidnummer:");

        buttonZoek.setText("Zoek");
        buttonZoek.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonZoekActionPerformed(evt);
            }
        });

        fieldResultaten.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N

        buttonVerwijder.setText("Verwijder");
        buttonVerwijder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonVerwijderActionPerformed(evt);
            }
        });

        buttonWijzig.setText("Wijzig");
        buttonWijzig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonWijzigActionPerformed(evt);
            }
        });

        buttonNieuw.setText("Nieuw");
        buttonNieuw.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonNieuwActionPerformed(evt);
            }
        });

        lblNaam.setText("Naam");

        lblGeslacht.setText("Geslacht");

        lblGeboortedatum.setText("  Geboortedatum");

        buttonTerug.setText("Terug");
        buttonTerug.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTerugActionPerformed(evt);
            }
        });

        lblOudernaam.setText("Naam:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(fieldResultaten, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(lblNaam)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 141, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(buttonNieuw)
                                    .addComponent(lblGeslacht, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addComponent(lblGeboortedatum, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(buttonZoek, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(buttonTerug, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(buttonWijzig)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(buttonVerwijder))))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblOudernaam)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(fieldOudernaam))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblOudercode)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(fieldOudercode, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblOudercode)
                    .addComponent(fieldOudercode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonZoek)
                    .addComponent(buttonTerug))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblOudernaam)
                    .addComponent(fieldOudernaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonVerwijder)
                    .addComponent(buttonWijzig)
                    .addComponent(buttonNieuw))
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNaam)
                    .addComponent(lblGeslacht)
                    .addComponent(lblGeboortedatum))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fieldResultaten, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonZoekActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonZoekActionPerformed
        String code = fieldOudercode.getText();
        fieldOudernaam.setText(Kind.getNaamOuder(Integer.parseInt(code)));
        if (code.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Oudercode is leeg!");
            }else{
            //haal kinderen op uit database op basis van de oudercode
            Object[] kinderen = Kind.getKinderen(code);
            //toon de opgehaalde gegevens
            fieldResultaten.fillList(kinderen);
        }  
    }//GEN-LAST:event_buttonZoekActionPerformed

    private void buttonWijzigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonWijzigActionPerformed
        Kind eenKind = getKind();
        if (eenKind != null){
            new KindToevoegen(eenKind).setVisible(true);
            this.dispose();
        }
    }//GEN-LAST:event_buttonWijzigActionPerformed

    private void buttonNieuwActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonNieuwActionPerformed
        String code = fieldOudercode.getText();
        if (code.isEmpty())
            JOptionPane.showMessageDialog(null, "Oudercode is leeg!");
        else {
            new KindToevoegen(code).setVisible(true);
                this.dispose();
        }
    }//GEN-LAST:event_buttonNieuwActionPerformed

    private void buttonVerwijderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonVerwijderActionPerformed
        Kind eenKind = getKind();
    if (eenKind != null) {
      int antwoord = JOptionPane.showConfirmDialog(null,"Wil je "+eenKind.getNaam()+" echt verwijderen?\n","verwijder kind",JOptionPane.YES_NO_OPTION);
      if (antwoord == JOptionPane.YES_OPTION) {
        if (eenKind.verwijder()) {
          JOptionPane.showMessageDialog(null, "Kind verwijderd!");
          fieldResultaten.fillList(Kind.getKinderen(fieldOudercode.getText()));
          }else {
            JOptionPane.showMessageDialog(null, "Probleem bij verwijderen kind.");
            }
          }    
        }
    }//GEN-LAST:event_buttonVerwijderActionPerformed

    private void buttonTerugActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTerugActionPerformed
        this.dispose();
    }//GEN-LAST:event_buttonTerugActionPerformed
    
    private Kind getKind() {//bekijkt of en zo ja welk kind geselecteerd is
        Kind eenKind = (Kind) fieldResultaten.getSelectedObject();    
        if (eenKind == null)
            JOptionPane.showMessageDialog(null, "Geen kind geselecteerd!");
        return eenKind;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Kindscherm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Kindscherm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Kindscherm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Kindscherm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Kindscherm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonNieuw;
    private javax.swing.JToggleButton buttonTerug;
    private javax.swing.JButton buttonVerwijder;
    private javax.swing.JButton buttonWijzig;
    private javax.swing.JButton buttonZoek;
    private javax.swing.JTextField fieldOudercode;
    private javax.swing.JTextField fieldOudernaam;
    private components.JSqlList fieldResultaten;
    private javax.swing.JLabel lblGeboortedatum;
    private javax.swing.JLabel lblGeslacht;
    private javax.swing.JLabel lblNaam;
    private javax.swing.JLabel lblOudercode;
    private javax.swing.JLabel lblOudernaam;
    private components.JSqlList studentList;
    // End of variables declaration//GEN-END:variables
}
